package com.caregiver.watch.business.user.service.impl;

import com.caregiver.watch.business.user.dao.UserContactDao;
import com.caregiver.watch.business.user.model.dto.AddContactParam;
import com.caregiver.watch.business.user.model.dto.EditContactParam;
import com.caregiver.watch.business.user.model.po.UserContact;
import com.caregiver.watch.business.user.model.vo.ContactVo;
import com.caregiver.watch.business.user.service.UserContactService;
import com.caregiver.watch.business.user.utils.UserInfoUtils;
import com.caregiver.watch.common.exception.BaseException;
import com.caregiver.watch.common.exception.business.BusinessException;
import com.caregiver.watch.common.utils.collection.ListUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.caregiver.watch.common.constant.NumberConstants.ZERO;

/**
 * @Description: 联系人服务
 * @Author 疆戟
 * @Date 2025/8/17 23:53
 * @Version 1.0
 */
@Service
@Slf4j
public class UserContactServiceImpl implements UserContactService {

    @Autowired
    private UserContactDao userContactDao;


    @Override
    public Boolean addContact(AddContactParam param) {
        log.info("新增联系人：{}", param.getContacts());
        if (CollectionUtils.isEmpty(param.getContacts())) {
            return Boolean.FALSE;
        }
        Long userId = UserInfoUtils.getUserId();
        LocalDateTime now = LocalDateTime.now();
        List<UserContact> userContacts = ListUtils.map(param.getContacts(), contact -> {
            UserContact userContact = new UserContact();
            userContact.setUserId(userId);
            userContact.setContactName(contact.getName());
            userContact.setPhoneNumber(contact.getPhone());
            userContact.setCreateTime(now);
            userContact.setUpdateTime(now);
            return userContact;
        });
        if (CollectionUtils.isEmpty(userContacts)) {
            return Boolean.FALSE;
        }
        try {
            userContactDao.batchInsertUserContact(userContacts);
            return Boolean.TRUE;
        } catch (Exception e) {
            log.error("批量新增用户联系人失败：{}", e.getMessage());
            return Boolean.FALSE;
        }
    }

    @Override
    public Boolean editContact(EditContactParam param) {
        log.info("编辑联系人：{}", param);
        Long id = param.getId();
        if (Objects.isNull(id)) {
            throw new BusinessException("联系人ID不能为空");
        }
        String name = param.getName();
        if (StringUtils.isBlank(name)) {
            throw new BusinessException("联系人名称不能为空");
        }
        String phone = param.getPhone();
        if (StringUtils.isBlank(phone)) {
            throw new BusinessException("联系人手机号不能为空");
        }
        UserContact userContact = new UserContact();
        userContact.setId(id);
        userContact.setContactName(name);
        userContact.setPhoneNumber(phone);
        return userContactDao.updateUserContact(userContact) > ZERO;
    }

    @Override
    public Boolean deleteContact(Long contactId) {
        if (Objects.isNull(contactId)) {
            throw new BusinessException("联系人ID不能为空");
        }
        return userContactDao.deleteUserContactById(contactId) > ZERO;
    }

    @Override
    public List<ContactVo> queryContacts() {
        Long userId = UserInfoUtils.getUserId();
        List<UserContact> userContacts = userContactDao.selectUserContactListByUserId(userId);
        if (CollectionUtils.isEmpty(userContacts)) {
            return new ArrayList<>();
        }
        return ListUtils.map(userContacts, ContactVo::build);
    }
}
